package com.jinzhi.common.filter;

import com.alibaba.fastjson.JSONObject;
import com.jinzhi.api.exception.IFastApiException;
import com.jinzhi.common.config.InvoiceConfig;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.common.utils.yongyou.InsertWithArray;
import com.jinzhi.jzweb.domain.InvoiceorderDO;
import com.jinzhi.jzweb.service.InvoiceorderService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * @program: YongYouButtFilter
 * @description:
 * @author: Kl
 * @create: 2019-12-6 16:39
 **/
@Slf4j
@Component
public class YongYouButtFilter implements Command {

    @Autowired
    private InvoiceorderService invoiceorderService;
    @Autowired
    private UserService userService;

    @Override
    @Transactional
    public boolean execute(Context context) throws Exception {
        ApiResult apiResult = (ApiResult) context.get(InvoiceConfig.API_RESULT);
        Map<String, String> invoiceMapInfo = (Map<String, String>) context.get("DATAINFO");
        Map<String, String> invoiceMap = (Map<String, String>) context.get(InvoiceConfig.INVOICE_MAP);
        Long  uid = (Long) context.get("userDo");
        UserDO userDO = userService.selectById(uid);
        //发送请求，获取返回信息
        try {
            String result = new InsertWithArray().callInvoiceApply(invoiceMap);
            if (StringUtils.isBlank(result)) {
                throw new IFastApiException(ApiEnumError.getConfFail.getCodeStr());
            }

            log.info("invoiceMapInfo=="+invoiceMapInfo);

            JSONObject jsonObject = JSONObject.parseObject(result);
            if (!"0000".equals(jsonObject.getString("code"))) {
                throw new Exception("用友发票返回信息: 开票失败");
            }else{
                //保存发票订单信息
                InvoiceorderDO invoiceOrder = new InvoiceorderDO();
                invoiceOrder.setServiceName(invoiceMapInfo.get("XMMC"));
                invoiceOrder.setTaxrate(invoiceMapInfo.get("SL"));
                invoiceOrder.setAdvalorem(Double.valueOf(invoiceMapInfo.get("bigMoney")));
                invoiceOrder.setMoney(0.0);
                invoiceOrder.setTaxratemoney(0.0);
                invoiceOrder.setType(1);
                invoiceOrder.setCreateDate(new Date());
                invoiceOrder.setUpdateDate(new Date());
                invoiceOrder.setCreator(userDO.getUsername());
                invoiceOrder.setUpdater(userDO.getUsername());
                invoiceOrder.setUid(userDO.getId());
                invoiceOrder.setInvoiceId(Long.parseLong(invoiceMapInfo.get("invoiceId")));
                invoiceOrder.setOrderId(invoiceMapInfo.get("buildList"));
                invoiceOrder.setBuildFpqqlsh(invoiceMapInfo.get("buildFpqqlsh"));
                invoiceOrder.setShareurl("0");
                invoiceOrder.setSharecode("0");
                invoiceorderService.insert(invoiceOrder);
            }

        } catch (Exception e) {
            e.printStackTrace();
            apiResult.setCode(InvoiceConfig.ERROR_CODE);
            apiResult.setMsg(e.getMessage());
        }
        return false;
    }
}
